package com.bnyy.medicalHousekeeper.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bnyy.common.Constant;
import com.bnyy.common.adapter.BaseNormalListAdapter;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.adapter.ArticleAdapter;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.Article;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/8
 **/
public class ArticleListActivity extends BaseActivityImpl {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.iv_no_data)
    ImageView ivNoData;
    @BindView(R.id.tv_no_data)
    TextView tvNoData;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private int page = 1;
    private int size = 20;
    private int goodsId;
    private int type;
    private int userId;
    private String url;
    private ArticleAdapter adapter;

    public static void show(Context context) {
        context.startActivity(new Intent(context, ArticleListActivity.class));
    }

    /**
     * @param context
     * @param goodsId 商品id
     * @param type    商品类型 1 普通商品 2 服务商品
     */
    public static void show(Context context, int goodsId, int type) {
        Intent intent = new Intent(context, ArticleListActivity.class);
        intent.putExtra("goodsId", goodsId);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    /**
     * 医养管家分享文章列表
     *
     * @param context
     * @param userId
     */
    public static void show(Context context, int userId) {
        Intent intent = new Intent(context, ArticleListActivity.class);
        intent.putExtra("userId", userId);
        context.startActivity(intent);
    }

    @Override
    public OnRefreshListener registerOnRefreshListener() {
        return new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getArticleList();
            }
        };
    }

    @Override
    public OnLoadMoreListener registerOnLoadMoreListener() {
        return new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getArticleList();
            }
        };
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_recycler_view;
    }

    @Override
    public String getTitleStr() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        goodsId = getIntent().getIntExtra("goodsId", -1);
        type = getIntent().getIntExtra("type", -1);
        userId = getIntent().getIntExtra("userId", -1);
        recyclerView.setBackgroundColor(Color.WHITE);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));

        adapter = new ArticleAdapter(mContext, new BaseNormalListAdapter.OnItemClickListener<Article>() {
            @Override
            public void onClick(View view, Article article, int position) {
                if (goodsId != -1) {
                    ArticleDetailActivity.showGoodsArticle(mContext, article, goodsId);
                } else {
                    ArticleDetailActivity.showByArticleId(mContext, article);
                }
            }
        });
        recyclerView.setAdapter(adapter);

        if (userId == -1) {
            if (goodsId == -1) {
                setTitleStr("图文分享");
                url = requestManager.getBaseUrl() + Constant.NetWork.BASE_URL_JAVA + "housekeeper/articleList";
            } else {
                setTitleStr("商品图文");
                url = requestManager.getBaseUrl() + Constant.NetWork.BASE_URL_JAVA + "housekeeper/goodsArticle";
            }
        } else {
            setTitleStr("图文列表");
            url = requestManager.getBaseUrl() + Constant.NetWork.BASE_URL_JAVA + "housekeeper/shareRecord";
        }

        mRefreshLayout.autoRefresh();
    }

    private void getArticleList() {
        HashMap<String, Object> params = new HashMap<>();
        params.put("page_index", page);
        params.put("page_size", size);

        if (userId == -1) {
            if (goodsId != -1) {
                params.put("id", goodsId);
                params.put("type", type);
            }
        } else {
            params.put("id", userId);
        }
        requestManager.request(requestManager.mJavaRetrofitService.getArticleList(url, RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<ArrayList<Article>>(mRefreshLayout) {
                    @Override
                    public void onSuccess(ArrayList<Article> articles) {
                        super.onSuccess(articles);

                        mRefreshLayout.setEnableLoadMore(articles.size() >= size);

                        if (page == 1) {
                            if (articles.size() == 0) {
                                llNoData.setVisibility(View.VISIBLE);
                            } else {
                                llNoData.setVisibility(View.GONE);
                            }
                            adapter.refresh(articles);
                        } else {
                            adapter.loadMore(articles);
                        }
                    }
                });
    }
}




































